package moni.anyou.com.view.view.adapter;
import android.content.Intent;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.net.Uri;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import moni.anyou.com.view.view.KindergartenFragment;
import moni.anyou.com.view.R;
import moni.anyou.com.view.bean.response.ResDynamicsBean;
import moni.anyou.com.view.base.BaseInfo;
import moni.anyou.com.view.config.SysConfig;
import moni.anyou.com.view.tool.Tools;
import moni.anyou.com.view.view.KindergartenFragment;
import moni.anyou.com.view.view.SearchActivity;
import moni.anyou.com.view.view.dynamics.SendDynamicActivity;
import moni.anyou.com.view.view.dynamics.detail.image.ImagePagerActivity;
import moni.anyou.com.view.webview.ShowWebActivity;
import moni.anyou.com.view.widget.pikerview.Utils.TextUtil;
import moni.anyou.com.view.widget.recycleview.DividerItemDecoration;
import moni.anyou.com.view.widget.recycleview.NoSMRecycleView;
import moni.anyou.com.view.view.dynamics.adapter.RecAdapter;

/**
 * Created by Administrator on 2016/11/21.
 */

public class IndexItemsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private KindergartenFragment mContext;
    private LayoutInflater mInflater;
    private ArrayList<ResDynamicsBean.ListBean> mItems = new ArrayList<>();
    private String[] countInfo = {"0","0","0"};

    private final static int HEAD = 0;    // 头布局
    private final static int ITEM = 1;    // item布局

    public IndexItemsAdapter(KindergartenFragment context) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext.mBaseActivity);
    }

    public IndexItemsAdapter(KindergartenFragment context, ArrayList<ResDynamicsBean.ListBean> mItems) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext.mBaseActivity);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder vholder, final int position) {
        BaseInfo baseInfo = new Gson().fromJson(SysConfig.userInfoJson.toString(), BaseInfo.class);
        if (getItemViewType(position) == HEAD) {
            IndexItemsAdapter.HeadHolder holder = (IndexItemsAdapter.HeadHolder) vholder;


            return;
        }

        IndexItemsAdapter.MyViewHold holder = (IndexItemsAdapter.MyViewHold) vholder;

        final ResDynamicsBean.ListBean temps = mItems.get(position);
        try {
           if (!"".equals(temps.smallpic)) {

            // RecAdapter tempRecAdapter = new RecAdapter(mContext, temps.pic.split(","), temps.pic.split(","));
            //  holder.rc_icon.setAdapter(tempRecAdapter);
            //  tempRecAdapter.setOnPositionClickListener(new RecAdapter.OnPositionClickListener() {
             //      @Override
            //  public void onItemClick(int position, String[] url) {
//
            //         imageBrower(position, url);
           //       }
            //    });
           }
           else{
                holder.rc_icon.setVisibility(View.GONE);
            }
        }
        catch (Exception ex){

        }
        mContext.setBitmaptoImageView11(SysConfig.PicUrl + temps.icon, holder.iv_headicon);
        holder.tv_sentTime.setText(Tools.main(temps.addtime));
        holder.tvnickname.setText(temps.nick);
        holder.tvRoleName.setText("(" + temps.rolename+")");

         holder.tv_dynamicsContant.setText(Tools.transferJsonToString(""));


        holder.tv_lots.setText(temps.likeuser);
        if (!TextUtil.isEmpty(temps.likeuser)) {
            holder.tv_lots.setText(Tools.getLikeNikeNameStr(temps.likeuser));
            holder.ll_mark.setVisibility(View.VISIBLE);
            holder.v_line.setVisibility(View.VISIBLE);
        } else {
            holder.ll_mark.setVisibility(View.GONE);
            holder.v_line.setVisibility(View.GONE);
        }
        holder.ivZan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //mContext.marklike(position, getItem(position));
                IPraiseClickListener.onItemClick(position, temps);
            }
        });
        holder.tvRoleName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("title", temps.nick);
                intent.putExtra("url", SysConfig.webUrl +"/web/teacher.html?id="+temps.userid);
                intent.setClass(mContext.getActivity(), ShowWebActivity.class);
                mContext.getActivity().startActivity(intent);
            }
        });
        holder.ll_linkname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    // Uri uri = Uri.parse(SysConfig.FileUrl+"/"+temps.url);
                    // Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    // mContext.getActivity().startActivity(intent);



            }
        });
        try {
            if (SysConfig.uid.equals(temps.userid) ) {
                holder.iv_delete_dynamics.setVisibility(View.VISIBLE);
            } else {
                holder.iv_delete_dynamics.setVisibility(View.GONE);
            }
        }
        catch (Exception ex){

        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext.mBaseActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.rc_contonts.setLayoutManager(linearLayoutManager);
        CommentAdapter adapter = new CommentAdapter(mContext, temps.commentList);
        holder.rc_contonts.setAdapter(adapter);
        holder.ivShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mComentClickListener != null) {
                    mComentClickListener.onItemClick(position, temps);
                }
            }
        });

        holder.iv_delete_dynamics.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mOnDeleteClickListener != null) {
                    mOnDeleteClickListener.onItemClick(position, temps);
                }

            }
        });


    }

    View mView;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == HEAD) {
            return null;
        }
        IndexItemsAdapter.MyViewHold holder = null;
        if (holder == null) {
            mView = mInflater.inflate(R.layout.item_dynamics, parent, false);
            holder = new IndexItemsAdapter.MyViewHold(mView);
            mView.setTag(holder);
        } else {
            holder = (IndexItemsAdapter.MyViewHold) mView.getTag();
        }
        return holder;
    }

    @Override
    public int getItemCount() {
        if (mItems != null) {
            return mItems.size();
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEAD;
        }
        return ITEM;
    }

    public void setDatas(){





    }

    public void AddDatas(ArrayList result) {
        if (mItems != null && result != null) {
            mItems.addAll(result);

        }
        notifyDataSetChanged();
    }


    private ResDynamicsBean.ListBean getItem(int position) {
        return mItems.get(position);
    }

    public void removeDynamics(int position) {
        mItems.remove(position);
        notifyDataSetChanged();
    }

    private class HeadHolder extends RecyclerView.ViewHolder {
        //CircleImageView cvHeadIcon;
        LinearLayout ll_dynamics;
        LinearLayout ll_demand;
        LinearLayout ll_uploadFile;
        RelativeLayout rl_search;
        TextView tv_count;
        private HeadHolder(View itemView) {
            super(itemView);
           // cvHeadIcon = (CircleImageView) itemView.findViewById(R.id.civ_headIcon);

        }
    }


    class MyViewHold extends RecyclerView.ViewHolder {
        CircleImageView iv_headicon;
        TextView tvnickname;
        TextView tv_dynamicsContant;
        NoSMRecycleView rc_icon;
        TextView tv_sentTime;
        ImageView ivZan;
        ImageView ivShare;
        TextView tv_lots;
        TextView tv_linkname;
        TextView tv_linkmobile;
        RelativeLayout ll_mark;
        RelativeLayout ll_linkname;
        TextView iv_delete_dynamics;
        NoSMRecycleView rc_contonts;
        TextView tvRoleName;
        View v_line;

        public MyViewHold(View itemView) {
            super(itemView);

            iv_headicon = (CircleImageView) itemView.findViewById(R.id.iv_headicon);
            ivShare = (ImageView) itemView.findViewById(R.id.iv_share);
            ivZan = (ImageView) itemView.findViewById(R.id.iv_zan);
            rc_icon = (NoSMRecycleView) itemView.findViewById(R.id.rc_icon);
            tv_sentTime = (TextView) itemView.findViewById(R.id.tv_sentTime);
            tvnickname = (TextView) itemView.findViewById(R.id.tv_nickName);
            tv_dynamicsContant = (TextView) itemView.findViewById(R.id.tv_dynamicsContant);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext.getContext(), 3);
            rc_icon.addItemDecoration(new DividerItemDecoration(mContext.getContext(), LinearLayoutManager.HORIZONTAL));
            rc_icon.addItemDecoration(new DividerItemDecoration(mContext.getContext(), LinearLayoutManager.VERTICAL));
            rc_icon.setLayoutManager(gridLayoutManager);
            tv_lots = (TextView) itemView.findViewById(R.id.tv_lots);

            iv_delete_dynamics = (TextView) itemView.findViewById(R.id.tv_delete_dynamics);
            ll_mark = (RelativeLayout) itemView.findViewById(R.id.ll_mark);


            rc_contonts = (NoSMRecycleView) itemView.findViewById(R.id.rc_contonts);
            v_line = itemView.findViewById(R.id.v_line);
            linearLayoutManager = new LinearLayoutManager(mContext.mContext,
                    LinearLayoutManager.VERTICAL, false) {
                @Override
                public boolean canScrollVertically() {
                    return true;
                }

            };
        }
    }


    private ComentClickListener mComentClickListener = null;
    private OnDeleteClickListener mOnDeleteClickListener = null;
    private IPraiseClickListener IPraiseClickListener = null;
    private DownloadClickListener DownloadClickListener = null;
    //define interface
    public static interface OnDeleteClickListener {
        void onItemClick(int position, ResDynamicsBean.ListBean data);
    }

    //define interface
    public static interface ComentClickListener {
        void onItemClick(int position, ResDynamicsBean.ListBean data);
    }

    public static interface IPraiseClickListener {
        void onItemClick(int position, ResDynamicsBean.ListBean data);
    }
    public static interface DownloadClickListener {
        void onItemClick(int position, ResDynamicsBean.ListBean data);
    }

    public void setOnDeleteClickListener(OnDeleteClickListener mOnItemClickListener) {
        this.mOnDeleteClickListener = mOnItemClickListener;
    }

    public void setComentClickListener(ComentClickListener comentClickListener) {
        this.mComentClickListener = comentClickListener;
    }

    public void setIPraiseClickListener(IPraiseClickListener iPraiseClickListener) {
        this.IPraiseClickListener = iPraiseClickListener;
    }

    public void setDownloadClickListener(DownloadClickListener downloadClickListener) {
        this.DownloadClickListener = downloadClickListener;
    }
    private void imageBrower(int position, String[] urls) {
        Intent intent = new Intent(mContext.mBaseActivity, ImagePagerActivity.class);
        // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
      //  Toast.makeText(mContext.getActivity(), "test..........", Toast.LENGTH_LONG).show();
        mContext.startActivity(intent);
    }


    LinearLayoutManager linearLayoutManager;

}
